/*     */ package com.zimbra.cs.util.tnef.mapi;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.util.tnef.IcalUtil;
/*     */ import java.io.IOException;
/*     */ import java.net.URISyntaxException;
/*     */ import java.text.ParseException;
/*     */ import net.fortuna.ical4j.data.ContentHandler;
/*     */ import net.fortuna.ical4j.data.ParserException;
/*     */ import net.fortuna.ical4j.model.Component;
/*     */ import net.fortuna.ical4j.model.ComponentList;
/*     */ import net.fortuna.ical4j.model.Property;
/*     */ import net.fortuna.ical4j.model.PropertyList;
/*     */ import net.fortuna.ical4j.model.TimeZone;
/*     */ import net.fortuna.ical4j.model.UtcOffset;
/*     */ import net.fortuna.ical4j.model.ValidationException;
/*     */ import net.fortuna.ical4j.model.component.Daylight;
/*     */ import net.fortuna.ical4j.model.component.Standard;
/*     */ import net.fortuna.ical4j.model.component.VTimeZone;
/*     */ import net.fortuna.ical4j.model.property.TzId;
/*     */ import net.fortuna.ical4j.model.property.TzOffsetFrom;
/*     */ import net.fortuna.ical4j.model.property.TzOffsetTo;
/*     */ import net.freeutils.tnef.RawInputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TimeZoneDefinition
/*     */ {
/*  84 */   static Log sLog = ZimbraLog.tnef;
/*     */   
/*     */ 
/*     */   private String timezoneName;
/*     */   
/*     */ 
/*     */   private TZRule effectiveRule;
/*     */   
/*     */ 
/*     */   private TimeZone theZone;
/*     */   
/*     */   private MapiPropertyId mpi;
/*     */   
/*     */ 
/*     */   public TimeZoneDefinition(MapiPropertyId mpi, RawInputStream ris)
/*     */     throws IOException
/*     */   {
/* 101 */     this.theZone = null;
/* 102 */     this.mpi = mpi;
/* 103 */     int MajorVersion = ris.readU8();
/* 104 */     int MinorVersion = ris.readU8();
/* 105 */     int cbHeader = ris.readU16();
/* 106 */     int ReservedInt = ris.readU16();
/* 107 */     int cchKeyName = ris.readU16();
/* 108 */     setTimezoneName(ris.readStringUnicode(cchKeyName * 2));
/* 109 */     int cRules = ris.readU16();
/* 110 */     if (sLog.isDebugEnabled()) {
/* 111 */       StringBuffer debugInfo = new StringBuffer();
/* 112 */       debugInfo.append("TimeZoneName=");
/* 113 */       debugInfo.append(getTimezoneName());
/* 114 */       if (mpi.equals(MapiPropertyId.PidLidAppointmentTimeZoneDefinitionStartDisplay)) {
/* 115 */         debugInfo.append("(PidLidAppointmentTimeZoneDefinitionStartDisplay)");
/* 116 */       } else if (mpi.equals(MapiPropertyId.PidLidAppointmentTimeZoneDefinitionEndDisplay)) {
/* 117 */         debugInfo.append("(PidLidAppointmentTimeZoneDefinitionEndDisplay)");
/* 118 */       } else if (mpi.equals(MapiPropertyId.PidLidAppointmentTimeZoneDefinitionRecur)) {
/* 119 */         debugInfo.append("(PidLidAppointmentTimeZoneDefinitionRecur)");
/*     */       }
/* 121 */       debugInfo.append(":\n");
/* 122 */       if (MajorVersion != 2) {
/* 123 */         debugInfo.append("    Unexpected MajorVersion=");
/* 124 */         debugInfo.append(MajorVersion);
/* 125 */         debugInfo.append("\n");
/*     */       }
/* 127 */       if (MinorVersion != 1) {
/* 128 */         debugInfo.append("    Unexpected MinorVersion=");
/* 129 */         debugInfo.append(MinorVersion);
/* 130 */         debugInfo.append("\n");
/*     */       }
/* 132 */       if (ReservedInt != 2) {
/* 133 */         debugInfo.append("    Unexpected Reserved=0x");
/* 134 */         debugInfo.append(Integer.toHexString(ReservedInt));
/* 135 */         debugInfo.append("\n");
/*     */       }
/* 137 */       debugInfo.append("    cbHeader=");
/* 138 */       debugInfo.append(cbHeader);
/* 139 */       debugInfo.append(" cchKeyName=");
/* 140 */       debugInfo.append(cchKeyName);
/* 141 */       debugInfo.append(" cRules=");
/* 142 */       debugInfo.append(cRules);
/* 143 */       sLog.debug(debugInfo);
/*     */     }
/* 145 */     for (int cnt = 0; cnt < cRules; cnt++) {
/* 146 */       TZRule currRule = new TZRule(ris);
/* 147 */       if (currRule != null)
/*     */       {
/*     */ 
/* 150 */         if (currRule.isEffectiveRule())
/*     */         {
/* 152 */           setEffectiveRule(currRule);
/* 153 */           break;
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public TimeZoneDefinition(RawInputStream ris, String tzName)
/*     */     throws IOException
/*     */   {
/* 170 */     this.theZone = null;
/* 171 */     setTimezoneName(tzName);
/* 172 */     if (sLog.isDebugEnabled()) {
/* 173 */       StringBuffer debugInfo = new StringBuffer();
/* 174 */       debugInfo.append("TimeZoneName=");
/* 175 */       debugInfo.append(getTimezoneName());
/* 176 */       debugInfo.append(" (from PidLidDescription):\n");
/* 177 */       sLog.debug(debugInfo);
/*     */     }
/* 179 */     TZRule currRule = new TZRule();
/* 180 */     currRule.setBias(IcalUtil.readI32(ris));
/* 181 */     currRule.setStandardBias(IcalUtil.readI32(ris));
/* 182 */     currRule.setDaylightBias(IcalUtil.readI32(ris));
/* 183 */     ris.readU16();
/* 184 */     currRule.setStandardDate(new SYSTEMTIME(ris));
/* 185 */     ris.readU16();
/* 186 */     currRule.setDaylightDate(new SYSTEMTIME(ris));
/* 187 */     setEffectiveRule(currRule);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public TimeZoneDefinition(String tzName, int bias, int standardBias, int daylightBias, SYSTEMTIME standardDate, SYSTEMTIME daylightDate)
/*     */     throws IOException
/*     */   {
/* 200 */     this.theZone = null;
/* 201 */     setTimezoneName(tzName);
/*     */     
/* 203 */     TZRule currRule = new TZRule();
/* 204 */     currRule.setBias(bias);
/* 205 */     currRule.setStandardBias(standardBias);
/* 206 */     currRule.setDaylightBias(daylightBias);
/* 207 */     currRule.setStandardDate(standardDate);
/* 208 */     currRule.setDaylightDate(daylightDate);
/* 209 */     setEffectiveRule(currRule);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void setTimezoneName(String timezoneName)
/*     */   {
/* 216 */     this.timezoneName = timezoneName;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String getTimezoneName()
/*     */   {
/* 223 */     return this.timezoneName;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void setEffectiveRule(TZRule effectiveRule)
/*     */   {
/* 230 */     this.effectiveRule = effectiveRule;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public TZRule getEffectiveRule()
/*     */   {
/* 237 */     return this.effectiveRule;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public TimeZone getTimeZone()
/*     */   {
/* 245 */     if (this.theZone != null) {
/* 246 */       return this.theZone;
/*     */     }
/* 248 */     if (this.effectiveRule == null) {
/* 249 */       this.theZone = new TimeZone(0, "Etc/UTC");
/* 250 */       return this.theZone;
/*     */     }
/*     */     
/* 253 */     if (!this.effectiveRule.hasDaylightSaving()) {
/* 254 */       this.theZone = new TimeZone(this.effectiveRule.getStandardUtcOffsetMillis(), getTimezoneName());
/*     */       
/* 256 */       return this.theZone;
/*     */     }
/*     */     
/* 259 */     UtcOffset stdOffset = this.effectiveRule.getStandardUtcOffset();
/* 260 */     UtcOffset dlOffset = this.effectiveRule.getDaylightUtcOffset();
/* 261 */     PropertyList vtzProps = new PropertyList();
/* 262 */     TzId myTzid = new TzId(getTimezoneName());
/* 263 */     vtzProps.add(myTzid);
/* 264 */     VTimeZone vtz = new VTimeZone(vtzProps);
/* 265 */     Standard stdComp = new Standard();
/* 266 */     stdComp.getProperties().add(this.effectiveRule.getStandardDtStart());
/* 267 */     stdComp.getProperties().add(this.effectiveRule.icalStandardRRule());
/* 268 */     TzOffsetFrom offsetFrom = new TzOffsetFrom(dlOffset);
/* 269 */     TzOffsetTo offsetTo = new TzOffsetTo(stdOffset);
/* 270 */     stdComp.getProperties().add(offsetFrom);
/* 271 */     stdComp.getProperties().add(offsetTo);
/* 272 */     Daylight dayComp = new Daylight();
/* 273 */     dayComp.getProperties().add(this.effectiveRule.getDaylightDtStart());
/* 274 */     dayComp.getProperties().add(this.effectiveRule.icalDaylightRRule());
/* 275 */     offsetFrom = new TzOffsetFrom(stdOffset);
/* 276 */     offsetTo = new TzOffsetTo(dlOffset);
/* 277 */     dayComp.getProperties().add(offsetFrom);
/* 278 */     dayComp.getProperties().add(offsetTo);
/* 279 */     vtz.getObservances().add(stdComp);
/* 280 */     vtz.getObservances().add(dayComp);
/*     */     try {
/* 282 */       vtz.validate(true);
/* 283 */       this.theZone = new TimeZone(vtz);
/*     */     } catch (ValidationException e) {
/* 285 */       if (sLog.isDebugEnabled()) {
/* 286 */         sLog.debug("Problem with property %s - will default to UTC" + this.mpi.toString(), e);
/*     */       }
/* 288 */       this.theZone = new TimeZone(0, "Etc/UTC");
/*     */     }
/* 290 */     this.theZone = new TimeZone(vtz);
/* 291 */     return this.theZone;
/*     */   }
/*     */   
/*     */   public void addVtimezone(ContentHandler icalOutput) throws ParserException, URISyntaxException, IOException, ParseException {
/* 295 */     if (getTimezoneName() == null) {
/* 296 */       return;
/*     */     }
/* 298 */     if (this.effectiveRule == null) {
/* 299 */       return;
/*     */     }
/* 301 */     getTimeZone();
/* 302 */     if (this.theZone == null) {
/* 303 */       return;
/*     */     }
/* 305 */     VTimeZone vtz = this.theZone.getVTimeZone();
/* 306 */     icalOutput.startComponent("VTIMEZONE");
/* 307 */     for (Object obj : vtz.getProperties()) {
/* 308 */       if ((obj instanceof Property)) {
/* 309 */         Property currProp = (Property)obj;
/* 310 */         IcalUtil.addProperty(icalOutput, currProp);
/*     */       }
/*     */     }
/* 313 */     for (Object obj : vtz.getObservances()) {
/* 314 */       if ((obj instanceof Component)) {
/* 315 */         Component currComp = (Component)obj;
/* 316 */         icalOutput.startComponent(currComp.getName());
/* 317 */         for (Object propObj : currComp.getProperties()) {
/* 318 */           if ((propObj instanceof Property)) {
/* 319 */             Property obsProp = (Property)propObj;
/* 320 */             IcalUtil.addProperty(icalOutput, obsProp);
/*     */           }
/*     */         }
/* 323 */         icalOutput.endComponent(currComp.getName());
/*     */       }
/*     */     }
/* 326 */     icalOutput.endComponent("VTIMEZONE");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/tnef/mapi/TimeZoneDefinition.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */